package controllers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import model.db.DataBase;
import model.dtos.Game;
import views.windows.CalendarPanel;

public class CalendarController extends CalendarPanel
{
	private static final long serialVersionUID = 1L;
	
	public CalendarController()
	{
		super();
		fillGrid();
	}
	
	void fillGrid()
	{
		List<Game> gamesPlayed = new ArrayList<Game>(DataBase.getSeason().getGames());
		
		Collections.sort(gamesPlayed, new Comparator<Game>() {
		
			public int compare(Game g1, Game g2)
			{
				return g1.getDate().compareTo(g2.getDate());
			}
		});
		
		String[][] data = new String[gamesPlayed.size()][6];
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Calendar calendar = Calendar.getInstance();
		String days[] = new String[] { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
		
		
		for (int i = 0; i < gamesPlayed.size(); ++i)
		{
			calendar.setTime(gamesPlayed.get(i).getDate());
			data[i][0] = df.format(gamesPlayed.get(i).getDate());
			data[i][1] = days[calendar.get(Calendar.DAY_OF_WEEK) - 1];
			data[i][2] = gamesPlayed.get(i).getHomeClub().getName();
			data[i][3] = String.valueOf(gamesPlayed.get(i).getRbisHomeClub());
			data[i][4] = gamesPlayed.get(i).getVisitor().getName();
			data[i][5] = String.valueOf(gamesPlayed.get(i).getRbisVisitor());
		}
				
		TableModel tableModel = new DefaultTableModel(data, new String[] { "Fecha", "Dia", "Home Club", "Carreras", "Visitante", "Anotadas" });
		getCalendarTable().setModel(tableModel);
		getCalendarTable().revalidate();
	}

}
